package com.doubletick.proxy.ipidea.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubletick.common.annotation.DataScope;
import com.doubletick.common.utils.DateUtils;
import com.doubletick.common.utils.spring.SpringUtils;
import com.doubletick.proxy.dto.IdNameDto;
import com.doubletick.proxy.ipidea.cache.UsernameCache;
import com.doubletick.proxy.ipidea.domain.ProxyAccount;
import com.doubletick.proxy.ipidea.service.IProxyAccountService;
import com.doubletick.proxy.ipidea.mapper.ProxyAccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 代理账号Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-08
 */
@Service
public class ProxyAccountServiceImpl extends ServiceImpl<ProxyAccountMapper, ProxyAccount> implements IProxyAccountService {
    @Autowired
    private ProxyAccountMapper proxyAccountMapper;



    /**
     * 查询代理账号
     *
     * @param id 代理账号主键
     * @return 代理账号
     */
    @Override
    public ProxyAccount selectProxyAccountById(Long id) {
        return proxyAccountMapper.selectProxyAccountById(id);
    }

    /**
     * 查询代理账号列表
     *
     * @param proxyAccount 代理账号
     * @return 代理账号
     */
    @Override
    @DataScope(deptAlias = "u", userAlias = "u")
    public List<ProxyAccount> selectProxyAccountList(ProxyAccount proxyAccount) {
        return setUsername(proxyAccountMapper.selectProxyAccountList(proxyAccount));
    }

    @Override
    @DataScope(deptAlias = "u", userAlias = "u")
    public List<IdNameDto> proxyAccountList(ProxyAccount proxyAccount) {
        return proxyAccountMapper.proxyAccountList(proxyAccount);
    }
    @Override
    @DataScope(deptAlias = "u", userAlias = "u")
    public List<IdNameDto> userList(ProxyAccount proxyAccount) {
        return proxyAccountMapper.userList(proxyAccount);
    }

    public static  List<ProxyAccount> setUsername(List<ProxyAccount> proxyAccounts) {
        proxyAccounts.forEach(proxyAccount -> {
            if (proxyAccount.getUsername() == null) {
                String username = SpringUtils.getBean(UsernameCache.class).getUsernameByUserId(proxyAccount.getUserId());
                if (StrUtil.isNotEmpty(username)) {
                    proxyAccount.setUsername(username);
                }
            }
        });
        return proxyAccounts;
    }

    /**
     * 新增代理账号
     *
     * @param proxyAccount 代理账号
     * @return 结果
     */
    @Override
    public int insertProxyAccount(ProxyAccount proxyAccount) {
        proxyAccount.setCreateTime(DateUtils.getNowDate());
        return proxyAccountMapper.insertProxyAccount(proxyAccount);
    }

    /**
     * 修改代理账号
     *
     * @param proxyAccount 代理账号
     * @return 结果
     */
    @Override
    public int updateProxyAccount(ProxyAccount proxyAccount) {
        return proxyAccountMapper.updateProxyAccount(proxyAccount);
    }

    /**
     * 批量删除代理账号
     *
     * @param ids 需要删除的代理账号主键
     * @return 结果
     */
    @Override
    public int deleteProxyAccountByIds(Long[] ids) {
        return proxyAccountMapper.deleteProxyAccountByIds(ids);
    }

    /**
     * 删除代理账号信息
     *
     * @param id 代理账号主键
     * @return 结果
     */
    @Override
    public int deleteProxyAccountById(Long id) {
        return proxyAccountMapper.deleteProxyAccountById(id);
    }

    @Override
    public List<ProxyAccount> queryProxyIdIsNull10min() {
        LambdaQueryWrapper<ProxyAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(ProxyAccount::getProxyId);
        queryWrapper.gt(ProxyAccount::getCreateTime, new Date(System.currentTimeMillis() - 10 * 60 * 1000L));
        //只查10分钟内的，防止一直查
        return this.list(queryWrapper);
    }


    @Override
    public ProxyAccount selectByProxyIdOrAccount(String proxyId, String account) {
        LambdaQueryWrapper<ProxyAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProxyAccount::getProxyId, proxyId);
        queryWrapper.or().eq(ProxyAccount::getAccount, account);
        return this.getOne(queryWrapper);
    }

    @Override
    public ProxyAccount selectByUserId(Long userId) {
        LambdaQueryWrapper<ProxyAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProxyAccount::getUserId, userId);
        return this.getOne(queryWrapper);
    }

    @Override
    public ProxyAccount selectByProxyId(String proxyId) {
        LambdaQueryWrapper<ProxyAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProxyAccount::getProxyId, proxyId);
        return this.getOne(queryWrapper);
    }


    @Override
    public boolean updateFlowById(ProxyAccount proxyAccount) {
        LambdaUpdateWrapper<ProxyAccount> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ProxyAccount::getConsumedFlow, proxyAccount.getConsumedFlow());
        if (StrUtil.isNotEmpty(proxyAccount.getProxyId())) {
            updateWrapper.set(ProxyAccount::getProxyId, proxyAccount.getProxyId());
        }
        updateWrapper.eq(ProxyAccount::getId, proxyAccount.getId());
        return this.update(updateWrapper);
    }
}
